package com.theydream.qeechain.model.map;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

import com.theydream.qeechain.R;
import com.theydream.qeechain.map.Map;

public class ItemProximityIntentReceiver extends BroadcastReceiver {
	private static final int NOTIFICATION_ID = 1000;
	
	@Override
	public void onReceive(Context context, Intent intent) {
	// Check whether I am entering or exiting the neighborhood around the alert location
	String key = LocationManager.KEY_PROXIMITY_ENTERING;
	Boolean entering = intent.getBooleanExtra(key, false);
	String item = intent.getStringExtra("item");
	if (entering) {
	Log.d(getClass().getSimpleName(), "entering");
	}
	else {
	Log.d(getClass().getSimpleName(), "exiting");
	}
	// Notification manager to enable display of the notification to the status bar
	NotificationManager notificationManager =
	(NotificationManager)
	context.getSystemService(Context.NOTIFICATION_SERVICE);
	// When the user clicks on the notification, then start the Alert activity
	Intent i = new Intent(context,Map.class);
	PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
	// Create a notification
	Notification notification = createNotification();
	notification.setLatestEventInfo(context, "Item Detected!","\""+ item+"\" is close to you.", pendingIntent);
	notificationManager.notify(NOTIFICATION_ID, notification);
	}
	private Notification createNotification() {
	Notification notification = new Notification();
	notification.icon = R.drawable.icon;
	notification.when = System.currentTimeMillis();
	notification.flags |= Notification.FLAG_AUTO_CANCEL;
	notification.flags |= Notification.FLAG_SHOW_LIGHTS;
	notification.defaults |= Notification.DEFAULT_VIBRATE;
	notification.defaults |= Notification.DEFAULT_LIGHTS;
	// Use the default user's alert notification sound
	// notification.defaults |= Notification.DEFAULT_SOUND;
	// Select the sound to be played back when the notification is delivered
	//notification.sound = Uri.parse("file:///sdcard/proximity-alert.mp3");
	notification.ledARGB = Color.WHITE;
	notification.ledOnMS = 1500;
	notification.ledOffMS = 1500;
	return notification;
	}
}
